﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using PhotoManagerApp.ViewModels;
using VisitorsApp.Views;

namespace PhotoManagerApp.Views
{
    public partial class ViewPhotos : Page
    {
        private MainViewModel viewModel;

        public ViewPhotos()
        {
            InitializeComponent();
            this.viewModel = new MainViewModel();
            this.DataContext = this.viewModel;
            WebContext.Current.Authentication.LoggedOut += new EventHandler<System.ServiceModel.DomainServices.Client.ApplicationServices.AuthenticationEventArgs>(Authentication_LoggedOut);
        }

        void Authentication_LoggedOut(object sender, System.ServiceModel.DomainServices.Client.ApplicationServices.AuthenticationEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Home", UriKind.Relative));
        }

        // Executes when the user navigates to this page.
        public void ButtonViewDetails_Click(object sender, RoutedEventArgs e)
        {
            PhotoDetails details = new PhotoDetails(this.viewModel);
            if (this.viewModel.CurrentImage.IsPublic)
            {
                details.Show();
            }
            else if (this.viewModel.LoggedInUser.Credits > 0)
            {
                if (!this.viewModel.CurrentImage.IsPublic)
                {
                    this.viewModel.LoggedInUser.Credits -= 1;
                    this.viewModel.SaveContext();
                }
                details.Show();
            }
            else
            {
                MessageBox.Show("Not enough credits");
            }
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (!WebContext.Current.User.IsAuthenticated)
            {
                NavigationService.Navigate(new Uri("/Home", UriKind.Relative));
            }
        }

    }
}
